/**
 * Created by fly on 2017/4/26.
 * 装修相关逻辑
 */
var db = require('../db/connect_pool');
var Q = require('q');
var fs = require('fs');
var config = require('../tools/config');

/**
 * 执行sql语句promise
 * @param sql
 * @param value
 * @returns {*|promise}
 */
//todo 应该写到一个单独的文件中
function doSqlPromise (sql,value) {
    var deferred = Q.defer();
    db.query(sql,value,function (result) {
        deferred.resolve(result);
    });
    return deferred.promise;
}

/**
 * 根据手机号获取用户id
 * @param tel
 * @returns {*|promise}
 */
function getUserId(tel) {
    var deferred = Q.defer();
    db.query('select id from z_user where tel = ?',[tel],function (result) {
        deferred.resolve(result);
    });
    return deferred.promise;
}


/**
 * 获取精装案例详细信息
 * @param id 案例的id
 * @param callback 回掉函数
 */
function getCaseDetail(id,callback) {
    var cases = {};
    doSqlPromise('select *,fitment_case.city as case_city,fitment_case.id as caseid from fitment_case ' +
        'left join z_fitment_company on(fitment_case.companyid = z_fitment_company.id) ' +
        'left join fitment_style on(fitment_style.id = fitment_case.styleid) ' +
        'left join z_user on(fitment_case.designerid = z_user.id)' +
        'where fitment_case.id = ?', [id])
        .then(function (result) {
            cases.mess = result;
        })
        .then(function () {
            doSqlPromise('select * from case_assess ' +
                'left join z_user on(case_assess.userid = z_user.id) ' +
                'where caseid = ?', [id])
                .then(function (assess) {
                    cases.assess = assess;
                });
        })
        .then(function () {
            doSqlPromise('select * from fit_case_detail where caseid = ?',[id])
                .then(function (detail) {
                    cases.detail = detail;
                    callback(cases);
                });
        });
}
exports.getCaseDetail = getCaseDetail;
/**
 * 获取商品详细信息
 * @param id 商品id
 * @param callback
 */
exports.getGoodsDetail = function (id, callback) {
    var goods = {};
    doSqlPromise('select * from fitment_goods where id = ?', [id])
        .then(function (result) {
            goods.mess = result;
        })
        .then(function () {
            doSqlPromise('select * from goods_assess ' +
                'left join z_user on(z_user.id = goods_assess.userid) ' +
                'where goodsid = ?', [id])
                //todo 商品评价  待完善
                .then(function (assess) {
                    goods.assess = assess;
                    callback(goods);
                });
        });
}
/**
 * 添加购物车
 * @param data
 * @param tel 登陆用户
 * @param id 商品id
 * @param num 数量
 * @returns {*|promise}
 */
exports.addCart = function (data) {
    var deferred = Q.defer();
    var userid;
    getUserId(data.tel)
        .then(function (user) {
            userid = user[0].id;
            doSqlPromise('insert into goods_cart(userid,goodsid,num) values(?,?,?)',[userid,data.id,data.num])
                .then(function (result) {
                    deferred.resolve(result);
                })
        });
    return deferred.promise;
}
/**
 * 获取攻略
 * @param data
 * @returns {*}
 */
exports.getStrategy = function (data) {
    var deferred = Q.defer();
    doSqlPromise('select * from fitment_strategy limit ?,5',[0])
        .then(function (result) {
            deferred.resolve(result);
        });
    return deferred.promise;
}
/**
 *
 * @param id
 */
exports.getAdvisorDetail = function (id) {
    var deferred = Q.defer();
    doSqlPromise('select * from fitment_strategy where id = ?', [id])
        .then(function (result) {
            console.log(result)
            var data=fs.readFileSync('../public/'+result[0].contentsrc,"utf-8");
            console.log(data);
            deferred.resolve(data);
        });
    return deferred.promise;
}